﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Windows.Forms;

namespace ZofX.Service.Manager
{
    public partial class FormMonitor : Form
    {
        ///服务控制器
        //private ServiceController sc;

        //服务名称
        private string serviceName;

        //设置exe文件名
        private string configExeName;

        public FormMonitor()
        {
            InitializeComponent();
            serviceName = string.IsNullOrEmpty(ConfigurationManager.AppSettings["ServiceName"]) ? "" : ConfigurationManager.AppSettings["ServiceName"];
            configExeName = string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConfigExeName"]) ? "" : ConfigurationManager.AppSettings["ConfigExeName"];
        }

        /// <summary>
        /// 窗体载入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMonitor_Load(object sender, EventArgs e)
        {
            //隐藏通知栏图标
            notifyIcon.Visible = false;
            timerCheck.Enabled = true;
        }

        /// <summary>
        /// 窗口关闭之前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
            //this.ShowInNotify(true);
        }

        /// <summary>
        /// 窗体大小发生变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormMonitor_SizeChanged(object sender, EventArgs e)
        {
            //如果最小化
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInNotify(true);
            }
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            Config();
        }

        /// <summary>
        /// 双击通知栏图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.ShowInNotify(false);
        }

        /// <summary>
        /// 开始或继续服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            Start();
        }

        /// <summary>
        /// 暂停服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            Pause();
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            Stop();
        }

        /// <summary>
        /// 退出右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            Application.Exit();
        }

        private void 启动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Pause();
        }

        private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stop();
        }

        private void 配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Config();
        }

        /// <summary>
        /// 100ms计时器事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerCheck_Tick(object sender, EventArgs e)
        {
            timerCheck.Enabled = false;
            Fresh();
        }

        /// <summary>
        /// 通知栏显示设置
        /// </summary>
        /// <param name="isSowInNotify">是否显示在通知栏</param>
        private void ShowInNotify(bool isShowInNotify)
        {
            //显示图标
            notifyIcon.Visible = isShowInNotify;

            //隐藏窗体
            this.ShowInTaskbar = !isShowInNotify;
            this.Visible = !isShowInNotify;
            if (this.Visible && this.WindowState == FormWindowState.Minimized)
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        private void Start()
        {
            toolStripStatusLabel.Text = "服务正在启动...";
            ServiceController sc = new ServiceController(serviceName);
            if (sc.Status == ServiceControllerStatus.Stopped)
            {
                sc.Start();
            }
            else if (sc.Status == ServiceControllerStatus.Paused)
            {
                sc.Continue();
            }
            sc.Close();
            sc.Dispose();

            toolStripStatusLabel.Text = "服务已启动";
            btnStart.Enabled = false;
            btnPause.Enabled = true;
            btnStop.Enabled = true;
            IconRightMenu.Items[1].Enabled = false;
            IconRightMenu.Items[2].Enabled = true;
            IconRightMenu.Items[3].Enabled = true;
        }

        private void Pause()
        {
            toolStripStatusLabel.Text = "服务正在暂停...";
            ServiceController sc = new ServiceController(serviceName);
            if (sc.Status == ServiceControllerStatus.Running)
            {
                sc.Pause();
            }
            sc.Close();
            sc.Dispose();

            toolStripStatusLabel.Text = "服务已暂停";
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            btnStop.Enabled = true;
            IconRightMenu.Items[1].Enabled = true;
            IconRightMenu.Items[2].Enabled = false;
            IconRightMenu.Items[3].Enabled = true;
        }

        private void Stop()
        {
            toolStripStatusLabel.Text = "服务正在停止...";
            ServiceController sc = new ServiceController(serviceName);
            if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.Paused)
            {
                try
                {
                    sc.Stop();
                }
                catch
                {
                    toolStripStatusLabel.Text = "服务无法停止";
                    sc.Close();
                    sc.Dispose();
                    return;
                }
            }
            sc.Close();
            sc.Dispose();

            toolStripStatusLabel.Text = "服务已停止";
            btnStart.Enabled = true;
            btnPause.Enabled = false;
            btnStop.Enabled = false;
            IconRightMenu.Items[1].Enabled = true;
            IconRightMenu.Items[2].Enabled = false;
            IconRightMenu.Items[3].Enabled = false;
        }

        private void Fresh()
        {
            if (string.IsNullOrEmpty(serviceName))
            {
                toolStripStatusLabel.Text = "服务未设置";
                return;
            }

            ServiceController sc = new ServiceController(serviceName);
            try
            {
                ServiceControllerStatus scStatus = sc.Status;
            }
            catch
            {
                timerCheck.Enabled = true;

                toolStripStatusLabel.Text = "服务未安装";
                btnStart.Enabled = false;
                btnPause.Enabled = false;
                btnStop.Enabled = false;
                IconRightMenu.Items[1].Enabled = false;
                IconRightMenu.Items[2].Enabled = false;
                IconRightMenu.Items[3].Enabled = false;

                sc.Close();
                sc.Dispose();
                sc = null;
                return;
            }
            timerCheck.Enabled = true;

            if (sc.Status == ServiceControllerStatus.Running)
            {
                toolStripStatusLabel.Text = "服务已启动";
                btnStart.Enabled = false;
                btnPause.Enabled = true;
                btnStop.Enabled = true;
                IconRightMenu.Items[1].Enabled = false;
                IconRightMenu.Items[2].Enabled = true;
                IconRightMenu.Items[3].Enabled = true;
            }
            else if (sc.Status == ServiceControllerStatus.Paused)
            {
                toolStripStatusLabel.Text = "服务已暂停";
                btnStart.Enabled = true;
                btnPause.Enabled = false;
                btnStop.Enabled = true;
                IconRightMenu.Items[1].Enabled = true;
                IconRightMenu.Items[2].Enabled = false;
                IconRightMenu.Items[3].Enabled = true;
            }
            else if (sc.Status == ServiceControllerStatus.Stopped)
            {
                toolStripStatusLabel.Text = "服务已停止";
                btnStart.Enabled = true;
                btnPause.Enabled = false;
                btnStop.Enabled = false;
                IconRightMenu.Items[1].Enabled = true;
                IconRightMenu.Items[2].Enabled = false;
                IconRightMenu.Items[3].Enabled = false;
            }
            sc.Close();
            sc.Dispose();
        }

        private void Config()
        {
            if (string.IsNullOrEmpty(configExeName))
            {
                toolStripStatusLabel.Text = "配置程序未设置";
                return;
            }
            string configExePath = Path.Combine(Application.StartupPath, configExeName);
            if (!File.Exists(configExePath))
            {
                toolStripStatusLabel.Text = "配置程序不存在";
                return;
            }
            Process.Start(configExePath);
        }
    }
}